from django.shortcuts import render,redirect
from django.urls import reverse
from magor.models import Student,College
# 学生展示页
def StudentList(request):
    stu=Student.objects.all()
    return render(request, 'slist.html', {'stu':stu})
# 学生添加
def StudenAdd(request):
    if request.method=="POST":
        name=request.POST.get('name')
        sex=request.POST.get('sex')
        age=request.POST.get('age')
        phone=request.POST.get('phone')
        magor=request.POST.get('magor')
        if sex=='0':
             sex='男'
        else:
            sex='女'
        Student.objects.create(name=name, sex=sex, age=age, phone=phone, magor_id=magor)
        return redirect('magor:slist')
    college=College.objects.all()
    return render(request, 'sadd.html', {'college':college})
# 学生修改
def Supdate(request,id):
    if request.method == "POST":
        name = request.POST.get('name')
        sex = request.POST.get('sex')
        age = request.POST.get('age')
        phone = request.POST.get('phone')
        magor = request.POST.get('magor')
        if sex == '0':
            sex = '男'
        else:
            sex = '女'
        Student.objects.filter(id=id).update(name=name, sex=sex, age=age, phone=phone, magor_id=magor)
        return redirect('magor:slist')
    stu=Student.objects.get(id=id)
    college = College.objects.all()
    return render(request, 'supdate.html',{'stu':stu,'college':college})
# 学生删除
def Sdelete(request,id):
    Student.objects.get(id=id).delete()
    return redirect("magor:slist")
# 专业展示页
def Clist(request):
    c=College.objects.all()
    return render(request,'clist.html',{'c':c})
# 专业添加
def CollegeAdd(request):
    if request.method=="POST":
        name = request.POST.get('name')
        College.objects.create(name=name)
        return redirect("magor:clist")
    return render(request,'cadd.html')
# 专业修改
def Cupdate(request,id):
    if request.method == "POST":
        name = request.POST.get('name')
        College.objects.filter(id=id).update(name=name)
        return redirect('magor:clist')
    magor=College.objects.get(id=id)
    return render(request,'')
# 专业删除
def Cdelete(request,id):
    College.objects.get(id=id).delete()
    return redirect("magor:clist")
